package com.cricketrandom;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CricketActivity extends Activity implements OnClickListener{

	private Button run;
	private TextView runlabel, runs, scorelabel, score, message, boundary;
	private int runValue, scoreValue = 0;
	private boolean isOut = false, halfCentury = false, century = false, four = false, six = false;
	private AlertDialog alertDialog;
	private String halfCenturyAlert = "HALF CENTURY!";
	private String centuryAlert = "CONGRATULATIONS! YOU SCORED A CENTURY!";
	private String outAlert = "OUT!";
	private String outForDuck = "Gone for a DUCK!";
	private String outInNineties = "OUT! Nervous Nineties!";
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cricket);
        run = (Button)findViewById(R.id.button1);
        run.setOnClickListener(this);
        runlabel = (TextView) findViewById(R.id.runlabel);
        runlabel.setTextColor(Color.WHITE);
        runs = (TextView) findViewById(R.id.textView2);
        scorelabel = (TextView) findViewById(R.id.scorelabel);
        scorelabel.setTextColor(Color.WHITE);
        score = (TextView) findViewById(R.id.textView4);
        //message = (TextView) findViewById(R.id.message);
        boundary = (TextView) findViewById(R.id.boundary);
        boundary.setTextColor(Color.parseColor("#8B0F5D"));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cricket, menu);
        return true;
    }


	@Override
	public void onClick(View view) {
		runValue = (int)(Math.random()*7);

		/*if(halfCentury){
			message.setText("");
			//halfCentury = false;
		}
		if(century){
			message.setText("");
			//century = false;
		}*/
		
		if(runValue == 4)
			boundary.setText("Four");
		else if(runValue == 6)
			boundary.setText("Six");
		else 
			boundary.setText(null);
		
		
		/*if(runValue == 0 && !isOut){
			message.setText("OUT!");
			run.setText("START NEW GAME");
			halfCentury = false;
			century = false;
		}
		else{
			message.setText("");
			run.setText("RUN");
		}
		
		if(isOut){
			scoreValue = runValue;
			//isOut = false;
			runs.setText("0");
			score.setText("0");
		}
		else{
			
			runs.setText(""+runValue);
			scoreValue = scoreValue + runValue;
			score.setText(""+scoreValue);
		}*/
		
		runs.setText(""+runValue);
		scoreValue = scoreValue + runValue;
		score.setText(""+scoreValue);
		
		if(runValue == 0){
			showMessage(scoreValue);
			runValue = 0;
			scoreValue = 0;
			//run.setText("RUN");
		}
		
		if(scoreValue >= 50 && !halfCentury && runValue != 0){
			/*message.setText(halfCenturyAlert);*/
			halfCentury = true;
			Toast toast = Toast.makeText(this, "Well done! Half Century!", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 20);
			toast.show();
		}
		if(scoreValue >= 100 && !century && runValue != 0){
			/*message.setText(centuryAlert);*/
			century = true;
			Toast toast = Toast.makeText(this, "Well done! Half Century!", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 20);
			toast.show();
		}
		/*if((runValue == 0) && !(isOut))
			isOut = true;
		else
			isOut = false;*/
		//Log.d("CricketActivity", ""+isOut+" "+runValue+" "+scoreValue);
	}

	public void showMessage(int finalScore){
		alertDialog = new AlertDialog.Builder(CricketActivity.this).create();
		if(finalScore == 0)
			alertDialog.setTitle(outForDuck);
		else if(finalScore >= 90 && finalScore < 100)
			alertDialog.setTitle(outInNineties);
		else
			alertDialog.setTitle(outAlert);
        alertDialog.setMessage("Your score is "+finalScore);
        alertDialog.setIcon(R.drawable.stumps);
        alertDialog.setButton("START NEW GAME", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            	score.setText("0");
            }
        });
        alertDialog.show();
	}
}
